package service;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import domain.AppDic;
import domain.AppDicRecord;

/**
 * @author wuzh
 * 
 */
public class AppDicService {

    private SessionFactory sessionFactory;

    /**
     * 获得所有的字典表
     * 
     * @return Collection<AppDic>
     */
    @SuppressWarnings("unchecked")
    public Collection<AppDic> getAllAppDic() {
        Session session = sessionFactory.getCurrentSession();
        String hsql = "from AppDic a";
        return session.createQuery(hsql).list();
    }

    /**
     * 创建一个字典表
     * @param appDic AppDic
     */
    public void createAppDic(AppDic appDic) {
        sessionFactory.getCurrentSession().save(appDic);
    }

    /**
     * 修改一个字典表
     * @param appDic AppDic
     */
    public void updateAppDic(AppDic appDic) {
        sessionFactory.getCurrentSession().update(appDic);
    }

    /**
     * 删除一个字典表
     * @param dicName Appdic
     */
    public void deleteAppDic(String dicName) {
        Session session = sessionFactory.getCurrentSession();
        String hsql = "delete from AppDic a where a.dicName=:dicName";
        session.createQuery(hsql).setString("dicName", dicName).executeUpdate();
    }

    /**
     * 根据唯一的dicName查询appDic
     * @param dicName 名字
     * @return AppDic
     */
    public AppDic getAppDicByDicName(String dicName) {
        return (AppDic) sessionFactory.getCurrentSession().get(AppDic.class,
                dicName);
    }

    /**
     * 获取某个字典表的Map集合
     * @param dicName 字典表名称
     * @return 典表的Map集合(map.key是字典记录的key,map.value是字典记录key对应的value)
     */
    public Map<String, String> getDicMap(String dicName) {
        if("sexMap".equalsIgnoreCase(dicName))
        {
            return makeSexMap();
        }else if("trueFalseMap".equalsIgnoreCase(dicName))
        {
            return makeTrueFalseMap();
        }
        Collection<AppDicRecord> records = getAppDicRecordByDicName(dicName);
        Map<String,String> mapData= new HashMap<String,String>();
        for (AppDicRecord recd : records) {
            mapData.put(recd.getRecKey(), recd.getRecValue());
        }
        return mapData;
    }

    private Map<String, String> makeTrueFalseMap() {
        Map<String,String> map=new LinkedHashMap<String,String>();
        map.put("1", "是");
        map.put("2", "否");
        return map;
    }

    private Map<String, String> makeSexMap() {
        Map<String,String> map=new LinkedHashMap<String,String>();
        map.put("1", "男");
        map.put("2", "女");
        return map;
    }

    /**
     * 根据dicName得到对应的字典表的记录的集合
     * 
     * @param dicName
     * @return Collection<AppDicRecord>
     */
    @SuppressWarnings("unchecked")
    public Collection<AppDicRecord> getAppDicRecordByDicName(String dicName) {
        String hsql = "from AppDicRecord a where a.dicName =:dicName";
        Collection<AppDicRecord> recordLst = sessionFactory.getCurrentSession()
                .createQuery(hsql).setString("dicName", dicName).list();
        return recordLst;
    }

    /**
     * 添加字典表的记录
     * 
     * @param appDicRecord 字典表的记录
     */
    public void createAutoGenDicRecord(AppDicRecord appDicRecord) {
        sessionFactory.getCurrentSession().save(appDicRecord);
    }

    /**
     * 更新 字典表的记录
     * 
     * @param appDicRecord
     */
    public void updateAutoGenDic(AppDicRecord appDicRecord) {
        sessionFactory.getCurrentSession().update(appDicRecord);
    }

    /**
     * 由id得到AppDicRecord
     * 
     * @param id 主键
     * @return AppDicRecord
     */
    public AppDicRecord getAutoGenDicRecordById(Integer id) {
        return (AppDicRecord) sessionFactory.getCurrentSession().get(
                AppDicRecord.class, id);
    }

    /**
     * 根据id删除对应的 AppDicRecord
     * 
     * @param id 主键
     */
    public void deleteAutoGenDicRecordById(Integer id) {
        Session session = sessionFactory.getCurrentSession();
        String hsql = "delete from AppDicRecord a where a.id=:id";
        session.createQuery(hsql).setInteger("id", id).executeUpdate();
    }

    /**
     * @param sessionFactory sessionFactory
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
